gusucode.com > VC++创建两个线程 非MFC-源码程序 > VC++创建两个线程 非MFC-源码程序/code/Q1.cpp

    // Q1.cpp : Defines the entry point for the console application.
// Download by http://www.NewXing.com

#include "stdafx.h"

const unsigned int cuStackSize = 128 * 1024;

class CThreadInfo
{
protected:
	HANDLE m_hInitialized;
	HANDLE m_hTerminate; 

public:
	CThreadInfo();
	~CThreadInfo();
	DWORD WaitForInitialized(DWORD dwWaitTime = 5000);
	DWORD WaitForTerminate(DWORD dwWaitTime = 5000);
	void SetInitialized();
	void SetTerminate();
};

CThreadInfo::CThreadInfo()
{
	// create two events
	//		no name, autoreset,
	//		not initially owned, default security

	m_hInitialized = CreateEvent(NULL, FALSE, FALSE, NULL);
	if (m_hInitialized != NULL)
	{
		m_hTerminate = CreateEvent(NULL, FALSE, FALSE, NULL);
		if (m_hTerminate == NULL)
		{
			CloseHandle(m_hInitialized);
			m_hInitialized = NULL;
		}
	}

	// an error? throw!
	if (m_hInitialized == NULL || m_hTerminate == NULL)
	{
		printf("ERROR: Couldn't create thread handles!\n");
		throw 1;
	}
}

CThreadInfo::~CThreadInfo()
{
	// if they exist, close them

	if (m_hInitialized == NULL)
		CloseHandle(m_hInitialized);
	if (m_hTerminate == NULL)
		CloseHandle(m_hTerminate);
}

DWORD CThreadInfo::WaitForInitialized(DWORD dwWaitTime /* = 5000 */)
{
	DWORD dwResult = WaitForSingleObject(m_hInitialized, dwWaitTime);

	if (dwWaitTime != 0 && dwResult == WAIT_TIMEOUT)
		throw 2;

	return dwResult;
}

DWORD CThreadInfo::WaitForTerminate(DWORD dwWaitTime /* = 5000 */)
{
	DWORD dwResult = WaitForSingleObject(m_hTerminate, dwWaitTime);

	if (dwWaitTime != 0 && dwResult == WAIT_TIMEOUT)
		throw 2;

	return dwResult;
}

void CThreadInfo::SetInitialized()
{
	SetEvent(m_hInitialized);
}

void CThreadInfo::SetTerminate()
{
	SetEvent(m_hTerminate);
}


unsigned __stdcall ProcOne(void* pThreadInfoObject)
{
	CThreadInfo* pInfo = (CThreadInfo*) pThreadInfoObject;

	try
	{
		// let our creator know we're initialized
		pInfo->SetInitialized();

		// wait until we're told to terminate
		pInfo->WaitForTerminate();
	}
	catch (int n)
	{
		printf("First thread ERROR: ");
		if (n == 2)
			printf("wait timeout!\n");
		else
			printf("some other error\n");
	}

	return 0;
}

unsigned __stdcall ProcTwo(void* pThreadInfoObject)
{
	CThreadInfo* pInfo = (CThreadInfo*) pThreadInfoObject;

	try
	{
		// let our creator know we're initialized
		pInfo->SetInitialized();

		MSG msg;
		int nIdleCount = 0;

		while (1)
		{
			if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
			{
				printf("Thread Two Received a message! Looped %d times\n", nIdleCount);
				fflush(stdout);
				nIdleCount = 0;
			}
			else
				nIdleCount++;

			if (pInfo->WaitForTerminate(0) != WAIT_TIMEOUT)
				break;
		}
	}
	catch (int n)
	{
		printf("Second thread ERROR: ");
		if (n == 2)
			printf("wait timeout!\n");
		else
			printf("some other error\n");
	}

	return 0;
}



int main(int argc, char* argv[])
{
	CThreadInfo* pInfo = NULL;

	try
	{
		// create a thread info objet to communicate with our threads...
		pInfo = new CThreadInfo;

		// data to identify threads
		HANDLE hFirstThread;
		DWORD dwFirstThread;
		HANDLE hSecondThread;
		DWORD dwSecondThread;

		// --- create the first thread
		hFirstThread = (HANDLE)	_beginthreadex(NULL, cuStackSize,
				ProcOne, pInfo, 0, (unsigned int*) &dwFirstThread);

		// wait for it to be initialized
		pInfo->WaitForInitialized();

		// try to post a message
		BOOL bResult = PostThreadMessage(dwFirstThread, WM_NULL, 0, 0);

		// print the results
		printf("First thread: PostThreadMessage() returns %d\n", bResult);

		// tell it to terminate
		pInfo->SetTerminate();

		// --- create the second thread
		hSecondThread = (HANDLE) _beginthreadex(NULL, cuStackSize,
				ProcTwo, pInfo, 0, (unsigned int*) &dwSecondThread);

		// wait for it to be initialized
		pInfo->WaitForInitialized();

		// try to post a message
		bResult = PostThreadMessage(dwSecondThread, WM_NULL, 0, 0);

		// print the results
		printf("Second thread: PostThreadMessage() returns %d\n", bResult);

		// let it run for a second
		Sleep(1000);

		// tell it to terminate
		pInfo->SetTerminate();
	}
	catch (int n)
	{
		if (n == 1)
			printf("ERROR: couldn't construct thread info object!\n");
		else if (n == 2)
			printf("ERROR: wait timeout!\n");
		else
			printf("ERROR: some other integer exception!\n");
	}
	catch (...)
	{
		printf("ERROR: Something is really bad!\n");
	}

	return 0;
}